"""Field

 [...]
"""
from jinja2 import Template

from models import items
from templates.controller import HEAD, ALL, FUNCTIONS, TEMPLATE


def controllergen():
    template = Template(TEMPLATE)
    
    codes = []

    codes.append(HEAD)

    tall = Template(ALL)
    sall = tall.render(lists=[item['classname'] for item in items])

    codes.append(sall)

    codes.append(FUNCTIONS)
    
    for item in items:
        title = item['title']
        classname = item['classname']
        tablename = item['tablename']
    
        code = template.render(title=title,
                               classname=classname,
                               tablename=tablename)
        codes.append(code)
    
    with open('gen/controller-sample.py', 'w') as f:
        s = "".join(codes)
        f.write(s)


if __name__ == '__main__':
    controllergen()
